package com.bincloud.vue.admin.web.service.ums.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bincloud.vue.admin.common.exception.BaseException;
import com.bincloud.vue.admin.common.exception.CodeMsg;
import com.bincloud.vue.admin.common.util.BaseEntityUtils;
import com.bincloud.vue.admin.common.util.ObjectUtils;
import com.bincloud.vue.admin.dao.mapper.ums.UmsUserRoleRelationMapper;
import com.bincloud.vue.admin.model.entity.UmsRole;
import com.bincloud.vue.admin.model.entity.UmsUser;
import com.bincloud.vue.admin.model.entity.UmsUserRoleRelation;
import com.bincloud.vue.admin.web.dto.ums.user.role.UmsUserRoleRelationReqDTO;
import com.bincloud.vue.admin.web.service.ums.IUmsRoleService;
import com.bincloud.vue.admin.web.service.ums.IUmsUserRoleRelationService;
import com.bincloud.vue.admin.web.service.ums.IUmsUserService;
import com.bincloud.vue.admin.web.util.request.RequestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Service
public class UmsUserRoleRelationServiceImpl extends ServiceImpl<UmsUserRoleRelationMapper, UmsUserRoleRelation> implements IUmsUserRoleRelationService {

    @Resource
    private IUmsUserService userService;
    @Resource
    private IUmsRoleService umsRoleService;
    @Resource
    private RequestService requestService;

    /**
     * 用户角色关系绑定
     *
     * @param reqDTO {@link UmsUserRoleRelationReqDTO}
     * @return 是否成功
     */
    @Override
    public boolean bind(UmsUserRoleRelationReqDTO reqDTO) {
        // 验证数据
        UmsUser user = getUser(reqDTO);
        UmsRole role = getRole(reqDTO);
        // 用户角色关系
        UmsUserRoleRelation relation = getUserRoleRelation(user.getId(), role.getId());
        if (ObjectUtils.isNotEmpty(relation)) {
            throw new BaseException(CodeMsg.UMS_ROLE_RELATION_EXISTED);
        }
        // 组装数据
        relation = new UmsUserRoleRelation();
        relation.setUid(user.getId());
        relation.setRid(role.getId());
        BaseEntityUtils.create(relation, requestService.getUsername());
        // 保存数据
        return this.save(relation);
    }

    /**
     * 用户角色关系解除
     *
     * @param reqDTO {@link UmsUserRoleRelationReqDTO}
     * @return 是否成功
     */
    @Override
    public boolean lift(UmsUserRoleRelationReqDTO reqDTO) {
        // 验证数据
        UmsUser user = getUser(reqDTO);
        UmsRole role = getRole(reqDTO);
        // 用户角色关系
        UmsUserRoleRelation relation = getUserRoleRelation(user.getId(), role.getId());
        if (ObjectUtils.isEmpty(relation)) {
            throw new BaseException(CodeMsg.UMS_ROLE_RELATION_NOT_EXIST);
        }
        // 删除数据
        return this.removeById(relation.getId());
    }

    private UmsUserRoleRelation getUserRoleRelation(Long uid, Long rid) {
        return this.getOne(Wrappers.<UmsUserRoleRelation>lambdaQuery().eq(UmsUserRoleRelation::getUid, uid).eq(UmsUserRoleRelation::getRid, rid));
    }

    private UmsRole getRole(UmsUserRoleRelationReqDTO reqDTO) {
        UmsRole role = umsRoleService.getRole(reqDTO.getCode());
        if (ObjectUtils.isEmpty(role)) {
            throw new BaseException(CodeMsg.UMS_ROLE_NOT_EXIST);
        }
        return role;
    }

    private UmsUser getUser(UmsUserRoleRelationReqDTO reqDTO) {
        UmsUser user = userService.getUser(reqDTO.getUsername());
        if (ObjectUtils.isEmpty(user)) {
            throw new BaseException(CodeMsg.UMS_USER_NOT_EXIST);
        }
        return user;
    }

}
